package com.example.group.network.express_plus_app;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.room.util.StringUtil;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;

import com.example.group.network.express_plus_app.Dao.AppDatabase;
import com.example.group.network.express_plus_app.Dao.UserDao.UserDao;
import com.example.group.network.express_plus_app.data.model.LoggedInUser;
import com.example.group.network.express_plus_app.ui.login.LoginViewModel;
import com.example.group.network.express_plus_app.ui.login.LoginViewModelFactory;
import com.example.group.network.express_plus_app.ui.mine.MineViewModel;
import com.example.group.network.express_plus_app.ui.mine.MineViewModelFactory;
import com.google.gson.annotations.Until;

public class SettingsActivity extends AppCompatActivity implements View.OnClickListener {
    AppDatabase appDatabase;
    private LoginViewModel loginViewModel;
    UserDao userDao=AppDatabase.getsInstance(SettingsActivity.this).userDao();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        appDatabase= AppDatabase.getsInstance(this);
        loginViewModel = new ViewModelProvider(this)
                .get(LoginViewModel.class);
        //标题栏设置
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        supportActionBar.setHomeAsUpIndicator(R.drawable.ic_baseline_arrow_back_ios_24);
        supportActionBar.setTitle("设置");
        //退出按钮
        findViewById(R.id.log_out).setOnClickListener(this);
        findViewById(R.id.my_address).setOnClickListener(this);
        findViewById(R.id.register_courier).setOnClickListener(this);
        findViewById(R.id.my_info).setOnClickListener(this);

    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId()==android.R.id.home){
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id==R.id.log_out){
            //退出登录
            logout();
            //跳转到首页
            Intent intent = new Intent(SettingsActivity.this, MainActivity.class);
            startActivity(intent);
        }else if (id==R.id.my_address){
            Intent intent = new Intent(SettingsActivity.this, SetDeliveryAddressActivity.class);
            startActivity(intent);
        }else if (id==R.id.register_courier){
            Intent intent = new Intent(SettingsActivity.this, RegisterCourierActivity.class);
            startActivity(intent);
        }else if (id==R.id.my_info){
            Intent intent = new Intent(SettingsActivity.this, PrivateInfoActivity.class);
            startActivity(intent);
        }
    }

    public void logout(){
        MainApplication mainApplication=MainApplication.getInstance();
        String phone = mainApplication.mInfoMap.get("phone");
        if (!TextUtils.isEmpty(phone)){
            LoggedInUser loggedInUser=new LoggedInUser(phone);
            userDao.delete(loggedInUser);
        }
        mainApplication.mInfoMap.remove("phone");
        mainApplication.mInfoMap.remove("token");
        mainApplication.mInfoMap.remove("user_face");
        mainApplication.mInfoMap.remove("net_name");
        mainApplication.mInfoMap.remove("delivery_address");
        //loginViewModel.logout();

    }

}